import math

from scipy.optimize import fsolve


def getJiaJiao(mid0, mid1):
    def getResult(jiao):
        k = mid1[0] - mid0[0] * math.cos(jiao) - mid0[1] * math.sin(jiao) + mid1[1] - mid0[0] * math.sin(jiao) + mid0[1] * math.cos(jiao)
        return k

    result = fsolve(getResult, 0.01)
    return result[0]


# 在一个板内找400个离散点
def isPengZhuang(dot1, dot2, dot3, dot4, L0, L1):
    mid0 = []
    mid1 = []
    mid0.append((dot1[0] + dot2[0]) / 2)
    mid0.append((dot1[1] + dot2[1]) / 2)
    mid1.append((dot3[0] + dot4[0]) / 2)
    mid1.append((dot3[1] + dot4[1]) / 2)
    jiaJiao = getJiaJiao(mid0, mid1)
    r0 = math.sqrt(mid0[0] ** 2 + mid0[1] ** 2)
    r1 = math.sqrt(mid1[0] ** 2 + mid1[1] ** 2)
    dx = (0.3 / 2) / 10
    dy = (L1 / 2) / 10
    arrDots = []
    for i in range(10):
        for j in range(10):
            arrDots.append([r1 - (i + 1) * dx, (j + 1) * dy])
    for i in range(10):
        for j in range(10):
            arrDots.append([r1 - (i + 1) * dx, -(j + 1) * dy])
    for i in range(10):
        for j in range(10):
            arrDots.append([r1 + (i + 1) * dx, -(j + 1) * dy])
    for i in range(10):
        for j in range(10):
            arrDots.append([r1 + (i + 1) * dx, (j + 1) * dy])
    for i in range(21):
        arrDots.append([r1, (10 - i) * dy])
    for i in range(21):
        arrDots.append([r1 + (i - 10) * dx, 0])

    for dot in arrDots:
        tempx = dot[0]
        tempy = dot[1]
        dot[0] = tempx * math.cos(jiaJiao) - tempy * math.sin(jiaJiao)
        dot[1] = tempx * math.sin(jiaJiao) + tempy * math.cos(jiaJiao)

    for dot in arrDots:
        if math.fabs(dot[1] - 0) <= (L0 / 2) and math.fabs(dot[0] - r0) <= (0.3 / 2):
            return True

    return False
